/**
 * Created by zhangqingxi on 2017-03-31.
 */


/**
 * 加载表格
 *
 */
function  loadDataList() {

    $('#dataList').datagrid({
        url:modelPath+'findByParam.shtml',
        queryParams:{
            sort:'d.addTime',
            order:'desc'
        },
        // idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},

            {field:'name',title:'物资名称',width:10,formatter:function (value,row,index) {
                return '<a href="javascript:void(0)" onclick="showDetails(\''+row.id+'\')" style="color: blue">'+value+'</a>'

            }},
            {field:'inventory',title:'库存量',width:10,formatter:function (value,row,index) {
                if(value===''||value===null){
                    return '0'
                }else {
                    return value;
                }

            }},
            {field:'measurementUnit',title:'计量单位',width:10},
            {field:'storagePointName',title:'存放点',width:10},
            {field:'deptName',title:'单位名称',width:10},
            {field:'bigTypeName',title:'物资大类',width:10},
            {field:'middleTypeName',title:'物资中类',width:10},
            {field:'smallTypeName',title:'物资小类',width:10},
            {field:'chargePersonName',title:'负责人',width:10},
            {field:'isOverdue',title:'是否涉及过期',width:10,formatter:function (value,row,index) {
                if(value===0){
                    return '否'
                }else if (value===1){
                    return '是'
                }

            }},
            {field:'addUserName',title:'登记人',width:10},
            {field:'addDeptName',title:'登记部门',width:10},
            {field:'d.addTime',title:'登记时间',width:10,sortable:true,formatter:function (value,row,index) {
                return row.addTime;
            }}
        ]],
        toolbar: '#toolbar'

    });


}

$(function () {
    loadDataList();
    //高级查询
    initAdvancedSearch();

    //查询按钮事件
    initSearchClick();

    //重置参数
    initResetClick();


    //添加按钮事件
    $('#btn_emergency_supplies_add').on('click',function () {

        openLayer("添加存放点",modelPath+"add.shtml");

    });
    //编辑按钮事件
    $('#btn_emergency_supplies_edit').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要修改的行！");
            return
        }
        var id=checkedRows[0].id;

        openLayer("编辑存放点",modelPath+"edit.shtml?id="+id);


    });

    //删除按钮事件
    $('#btn_emergency_supplies_delete').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要删除的行！");
            return
        }

        var id=checkedRows[0].id;
        initDeleteClick(modelPath,id);

    });
    //入库
    $('#btn_emergency_supplies_inStock').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要入库的行！");
            return
        }

        var id=checkedRows[0].id;
        openLayer("入库",modelPath+"inStock.shtml?id="+id);

    });
    //出库
    $('#btn_emergency_supplies_outStock').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要出库的行！");
            return
        }
        if(checkedRows[0].inventory === 0){
            layer.alert("库存量为 <span style='color: red;'>0</span> 无法出库！");
            return
        }

        var id=checkedRows[0].id;
        openLayer("出库",modelPath+"outStock.shtml?id="+id);

    });
    //盘点
    $('#btn_emergency_supplies_inventory').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要盘点的行！");
            return
        }

        var id=checkedRows[0].id;
        openLayer("盘点",modelPath+"inventory.shtml?id="+id);


    });





});
function showDetails(id) {

    openLayer("存放点详情",modelPath+"details.shtml?id="+id);

}